<?php

// 完善个人信息
// PUT member.info.complete

namespace app\api\resource\member\info;
use think\Db;
use think\Loader;
use think\Session;

class Complete
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['login'];
    // 描述
    public $_description = '完善个人信息';
    // 参数
    public $_param  = [
        'nickname'   => '昵称',
        'headimgurl' => '头像',
        'company'    => '公司',
        'position'   => '职位',
        // 'mobile'     => '手机号',
        'wechat'     => '微信号',
        'email'      => '邮箱',
        'province'   => '省',
        'city'       => '市',
    ];
    // 返回
    public $_return = [
        201 => '完善成功',
        422 => '请求参数不合法',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $params['nickname']   = $request->put('nickname/s','','trim');
        $params['headimgurl'] = $request->put('headimgurl/s','','trim');
        $params['company']    = $request->put('company/s','','trim');
        $params['position']   = $request->put('position/s','','trim');
        // $params['mobile']     = $request->put('mobile/s','','trim'); // 不可编辑
        $params['wechat']     = $request->put('wechat/s','','trim');
        $params['email']      = $request->put('email/s','','trim');
        $params['qq']         = $request->put('qq/s','','trim');
        $params['province']   = $request->put('province/s','','trim');
        $params['city']       = $request->put('city/s','','trim');

        $validate = Loader::validate('MemberInfo');
        if ( !$validate->scene('complete')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $memberId = Session::get('member.id');

        $num = Db::name('member_info')->where('member_id',$memberId)->update($params);
        return ($num >= 0) ? [ 201,[] ] : 500;
    }
}